#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main()
{
        int n, m, s, t;
        cin >> n >> m >> s >> t;
        vector<int> head(n + 1, 0);
        vector<int> next((m << 1) + 1, 0);
        vector<int> to((m << 1) + 1, 0);
        vector<int> weight((m << 1) + 1, 0);

        vector<int> dis(n + 1, INT32_MAX);
        vector<int> vis(n + 1, 0);

        int edge = 1;
        for(int i = 0; i < m; i++){
                int u, v, c;
                cin >> u >> v >> c;
                next[edge] = head[u];
                to[edge] = v;
                weight[edge] = c;
                head[u] = edge++;

                next[edge] = head[v];
                to[edge] = u;
                weight[edge] = c;
                head[v] = edge++;
        }

        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
        dis[s] = 0;
        pq.emplace(0, s);

        int ans = -1;
        while(!pq.empty()){
                auto [c, u] = pq.top();
                pq.pop();
                if(vis[u]){
                        continue;
                }
                vis[u] = 1;

                if(u == t){
                        ans = c;
                        break;
                }

                for(int e = head[u]; e; e = next[e]){
                        int v = to[e], w = weight[e];
                        if(!vis[v] && c + w < dis[v]){
                                dis[v] = c + w;
                                pq.emplace(dis[v], v);
                        }
                }
        }
        
        cout<<ans<<endl;
        return 0;
}